package basicutil.common;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;

public class ListUtil {
    public static <U, R> List<R> map(Collection<U> list, Function<U, R> func) {
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }
        List<R> r = new ArrayList<>();
        for (U u : list) {
            r.add(func.apply(u));
        }
        return r;
    }

    public static <U> List<U> filter(Collection<U> list, Predicate<U> func) {
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }
        List<U> r = new ArrayList<>();
        for (U u : list) {
            boolean test = func.test(u);
            if (test) {
                r.add(u);
            }
        }
        return r;
    }

    public static int sum(int... param) {
        int re = 0;
        for (int j : param) {
            re += j;
        }
        return re;
    }
}
